//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// Change zoom level settings.
//

# include "StdAfx.h"
# include "ChangeLevel.h"
# include "UndoRedo.h"
# include "MapView.h"
# include "Status.h"
# include "Scale.h"

class CChangeLevelAction : public CEditAction {
  public:
	CChangeLevelAction (size_t _cLevel, BYTE _btBits, BYTE _btBitsRangeTo, size_t _cZoom) :
		m_cLevel (_cLevel),
		m_btBitsOld        (g_map.Levels [_cLevel].btBits),
		m_btBitsRangeToOld (g_map.Levels [_cLevel].btBitsRangeTo),
		m_cZoomOld         (g_map.Levels [_cLevel].cZoom),
		m_btBits        (_btBits),
		m_btBitsRangeTo (_btBitsRangeTo),
		m_cZoom         (_cZoom)
	{}

	virtual void GetName (string_t & _str) const {
		_str.append ("Change level ");
		_str.append (1, '0' + m_cLevel);
	}
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo () {CChangeLevelAction::Do ();}

  private:
	const size_t m_cLevel;
	const BYTE m_btBits, m_btBitsOld;
	const BYTE m_btBitsRangeTo, m_btBitsRangeToOld;
	const size_t m_cZoom, m_cZoomOld;
};

bool CChangeLevelAction::Do () {
	CCritSec::CLock lock (g_cs);

	assert (m_cLevel < g_map.Levels.size ());

	map_level_t & level = g_map.Levels [m_cLevel];
	level.btBits        = m_btBits;
	level.btBitsRangeTo = m_btBitsRangeTo;
	level.cZoom         = m_cZoom;

	return true;
}

void CChangeLevelAction::Undo () {
	CCritSec::CLock lock (g_cs);

	assert (m_cLevel < g_map.Levels.size ());

	map_level_t & level = g_map.Levels [m_cLevel];
	level.btBits        = m_btBitsOld;
	level.btBitsRangeTo = m_btBitsRangeToOld;
	level.cZoom         = m_cZoomOld;
}

////////////////////////////////////////////

void ChangeLevel (size_t _cLevel, BYTE _btBits, BYTE _btBitsRangeTo, size_t _cZoom) {
	assert (_cLevel < g_map.Levels.size ());

	PerformEditAction (new CChangeLevelAction (_cLevel, _btBits, _btBitsRangeTo, _cZoom));

	Refresh ();
	SetScaleStatus (g_fScale);
}
